package com.ruoyi.web.controller.business;

import com.ruoyi.business.bo.HcxOrderEvaluateAddBo;
import com.ruoyi.business.bo.HcxOrderEvaluateEditBo;
import com.ruoyi.business.bo.HcxOrderEvaluateQueryBo;
import com.ruoyi.business.service.IHcxOrderEvaluateService;
import com.ruoyi.business.vo.HcxOrderEvaluateVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 订单评价Controller
 *
 * @author wsdz
 * @date 2022-08-18
 */
@Api(value = "订单评价控制器", tags = {"订单评价管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/business/OrderEvaluate")
public class HcxOrderEvaluateController extends BaseController {

    private final IHcxOrderEvaluateService iHcxOrderEvaluateService;

    /**
     * 查询订单评价列表
     */
    @ApiOperation("查询订单评价列表")
    @PreAuthorize("@ss.hasPermi('business:OrderEvaluate:list')")
    @GetMapping("/list")
    public TableDataInfo<HcxOrderEvaluateVo> list(@Validated HcxOrderEvaluateQueryBo bo) {
        return iHcxOrderEvaluateService.queryPageList(bo);
    }

    /**
     * 导出订单评价列表
     */
    @ApiOperation("导出订单评价列表")
    @PreAuthorize("@ss.hasPermi('business:OrderEvaluate:export')")
    @Log(title = "订单评价", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated HcxOrderEvaluateQueryBo bo) {
        List<HcxOrderEvaluateVo> list = iHcxOrderEvaluateService.queryList(bo);
        ExcelUtil<HcxOrderEvaluateVo> util = new ExcelUtil<>(HcxOrderEvaluateVo.class);
        return util.exportExcel(list, "订单评价");
    }

    /**
     * 获取订单评价详细信息
     */
    @ApiOperation("获取订单评价详细信息")
    @PreAuthorize("@ss.hasPermi('business:OrderEvaluate:query')")
    @GetMapping("/{id}")
    public AjaxResult<HcxOrderEvaluateVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iHcxOrderEvaluateService.queryById(id));
    }

    /**
     * 新增订单评价
     */
    @ApiOperation("新增订单评价")
    @PreAuthorize("@ss.hasPermi('business:OrderEvaluate:add')")
    @Log(title = "订单评价", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody HcxOrderEvaluateAddBo bo) {
        return toAjax(iHcxOrderEvaluateService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改订单评价
     */
    @ApiOperation("修改订单评价")
    @PreAuthorize("@ss.hasPermi('business:OrderEvaluate:edit')")
    @Log(title = "订单评价", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody HcxOrderEvaluateEditBo bo) {
        return toAjax(iHcxOrderEvaluateService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除订单评价
     */
    @ApiOperation("删除订单评价")
    @PreAuthorize("@ss.hasPermi('business:OrderEvaluate:remove')")
    @Log(title = "订单评价" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iHcxOrderEvaluateService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入订单评价数据
     */
    @ApiOperation("导入订单评价数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('business:OrderEvaluate:import')")
    @Log(title = "订单评价" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<HcxOrderEvaluateVo> util = new ExcelUtil<>(HcxOrderEvaluateVo.class);
        List<HcxOrderEvaluateVo> hcxOrderEvaluateList;
        try {
            hcxOrderEvaluateList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iHcxOrderEvaluateService.importHcxOrderEvaluate(hcxOrderEvaluateList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<HcxOrderEvaluateVo> util = new ExcelUtil<>(HcxOrderEvaluateVo.class);
        return util.importTemplateExcel("订单评价数据");
    }
}
